﻿using System;
using Skynet.CommonLibrary.ThreadRefreshTime.Interface;

namespace Skynet.CommonLibrary.ThreadRefreshTime.RefreshElement
{
    /// <summary>
    /// 复杂的任务处理元素
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-03-28
    /// </remarks>
    public class TaskTimerElement:IRefreshTaskTimer
    {
        public TaskTimerElement(int interval)
        {
            RefreshInterval = interval;
        }
        #region IRefreshTaskTimer 成员

        public int RefreshNumber
        {
            get;
            set;
        }


        public int CurrentRefreshNumber
        {
            get;
            set;
        }

        public Func<DateTime,bool> TaskConditions
        {
            get;
            set;
        }

        public DateTime TaskStartTime
        {
            get;
            set;
        }

        public DateTime TaskEndTime
        {
            get;
            set;
        }

        /// <summary>
        /// 验证个别属性
        /// </summary>
        public void Verify()
        {
            if (string.IsNullOrEmpty(this.RefreshID))
            {
                throw new Exception("编号不能为空");
            }
            if (TaskStartTime != DateTime.MinValue && TaskEndTime != DateTime.MinValue)
            {
                if (TaskStartTime >= TaskEndTime)
                {
                    throw new Exception("结束任务时间必须大于开始任务时间");
                }
            }
            if (TaskEndTime != DateTime.MinValue && TaskEndTime <= DateTime.Now)
            {
                throw new Exception("结束时间必须大于当前时间");
            }
        }
        #endregion

        #region IRefreshTimer 成员

        public string RefreshID
        {
            get;
            set;
        }

        public int RefreshInterval
        {
            get;
            set;
        }

        public Action<Action<IRefreshTimer, Exception>> Refresh
        {
            get;
            set;
        }

        public DateTime ExecutionTime
        {
            get;
            set;
        }

        public DateTime LastRefreshTime
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        #endregion
    }
}
